package com.sky.service.impl;
import com.sky.constant.MessageConstant;
import com.sky.exception.EmailException;
import com.sky.service.CommonService;
import com.sky.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class CommonServiceImpl implements CommonService {
  public String sendVerifycode(String email) {
    //邮箱不合法
    if (!MailUtils.checkMailVaild(email)) {
      throw new EmailException(MessageConstant.EMAIL_NOT_VAILD);
    }

    String verifyCode = MailUtils.generateVerifyCode(6);
    // 邮箱已经发送过验证码
    if(MailUtils.expiringMap.get(email)!=null){
      throw new EmailException(MessageConstant.EMAIL_ALREADY_SEND);
    }

    //发送消息失败
    boolean flag = MailUtils.sendQQMail(email, verifyCode);

    if (!flag) {
      throw new EmailException(MessageConstant.SEND_EMAIL_FAILED);
    }
    // 将验证码存放到map中
    MailUtils.expiringMap.put(email,verifyCode);
    return verifyCode;
  }
}
